package com.dengdai.applibrary.activitys;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ScrollView;

import com.dengdai.applibrary.BaseApplication;
import com.dengdai.applibrary.config.ResIdName;
import com.dengdai.applibrary.config.ViewInfo;
import com.dengdai.applibrary.fragments.BaseFragment;
import com.dengdai.applibrary.interfaces.ScrollViewInterface;
import com.dengdai.applibrary.utils.BaseViewUtils;
import com.dengdai.applibrary.widgets.custom.LoadDataEmptyView;
import com.dengdai.pullrefresh.library.PullToRefreshBase;
import com.dengdai.pullrefresh.library.PullToRefreshScrollView;

/**
 * @说明 刷新ScrollView
 * @作者 LY
 * @时间 16/5/24 11:23
 * @邮箱 2743569843@qq.com
 * @版权 Copyright(c) 2016 LY-版权所有
 * @备注
 */
public abstract class BaseScrollViewActivity extends BaseActivity implements ScrollViewInterface {
    protected PullToRefreshScrollView refreshScrollView;
    protected LoadDataEmptyView loadDataEmpty;
    protected BaseFragment baseFragment;
    private FragmentTransaction fragmentTransaction;
    private FragmentManager fragmentManager;

    @Override
    public int getLayoutResID() {
        return BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.LAYOUT, ViewInfo.BASE_SCROLLVIEW_ACTIVITY);
    }

    @Override
    public void initData() {
        refreshScrollView = (PullToRefreshScrollView) findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.REFRESHSCROLLVIEW));
        loadDataEmpty = (LoadDataEmptyView) findViewById(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.LOADDATAEMPTY));
    }

    @Override
    public void business() {
        //设置加载模式
        if (getScrollViewMode() != null) {
            refreshScrollView.setMode(getScrollViewMode());
        } else {
            refreshScrollView.setMode(PullToRefreshBase.Mode.BOTH);
        }
        //设置Fragment
        baseFragment = getContentFragment();
        if (baseFragment != null) {
            loadDataEmpty.setVisibility(View.GONE);
            fragmentManager = getSupportFragmentManager();
            fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(BaseViewUtils.gainResId(BaseApplication.getAppContext(), BaseViewUtils.ID, ResIdName.FRAGMENTCONTENT), baseFragment);
            fragmentTransaction.commit();
        }
        //设置刷新和加载
        refreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ScrollView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ScrollView> refreshView) {//刷新
                refresh();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ScrollView> refreshView) {//加载更多
                loadingMore();
            }
        });

    }

    @Override
    public void stopRefreshView() {
        refreshScrollView.onRefreshComplete();
        if (baseFragment != null) {
            loadDataEmpty.setVisibility(View.GONE);
        } else {
            loadDataEmpty.setVisibility(View.VISIBLE);
            loadDataEmpty.loadDataError();
        }
    }
}
